package com.autumn.config.client.trace;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.core.env.Environment;

import com.autumn.mq.event.EventMessage;
import com.autumn.mq.event.IEventHandler;
import com.autumn.mq.event.IMqEventBus;
import com.autumn.mq.rabbit.register.AutumnRabbitConnectionInfo;
import com.autumn.spring.boot.properties.AutumnCloudProperties;

/**
 * 配置跟踪处理器
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2018-01-25 22:28:18
 */
public class AutumnCloudConfigTraceHandler implements IEventHandler<AutumnCloudConfigTraceMessage> {

	private final static Log logger = LogFactory.getLog(AutumnCloudConfigTraceHandler.class);

	private String serviceId;

	/**
	 * 
	 * @param mqEventBus
	 */
	public AutumnCloudConfigTraceHandler(IMqEventBus mqEventBus, AutumnRabbitConnectionInfo info, String serviceId) {
		this.serviceId = serviceId;
		// 防止同一实例未注入
		mqEventBus.getFactory().registerConfigure(info.getEventHandlerInfos(), info.getEventDataInfos());
	}

	@Autowired
	private RefreshEndpoint refreshEndpoint;

	@Autowired
	private Environment env;

	@Override
	public boolean errorAutoRepeatSend() {
		return false;
	}

	@Override
	public Log getLogger() {
		return logger;
	}

	@Override
	public void onHandler(EventMessage<AutumnCloudConfigTraceMessage> message) {
		try {
			AutumnCloudConfigTraceMessage msg = message.getData();
			if (msg == null || this.serviceId == null) {
				return;
			}
			if (!this.serviceId.equalsIgnoreCase(msg.getServiceId())) {
				return;
			}
			if (msg.getHandleType() == null) {
				msg.setHandleType(AutumnCloudConfigTraceMessage.HANDLE_TYPE_REFRESH);
			}
			if (msg.getName() == null) {
				msg.setName("pro");
			}
			if (msg.getLabel() == null) {
				msg.setLabel("master");
			}
			logger.info(this.serviceId + " 配置已有变更(profile = " + msg.getName() + " label = " + msg.getLabel() + ")");
			String profile = env.getProperty(AutumnCloudProperties.Config.PREFIX_CONFIG + ".profile");
			if (profile == null) {
				profile = "pro";
			}
			String label = env.getProperty(AutumnCloudProperties.Config.PREFIX_CONFIG + ".label");
			if (label == null) {
				label = "master";
			}
			if (msg.getName().equalsIgnoreCase(profile) && msg.getLabel().equalsIgnoreCase(label)) {
				switch (msg.getHandleType()) {
				case AutumnCloudConfigTraceMessage.HANDLE_TYPE_REFRESH:
					this.refreshConfigure();
					break;
				case AutumnCloudConfigTraceMessage.HANDLE_TYPE_RESTART:
					logger.info("正在重新启动...");
					logger.info("暂不支持重启,改为刷新...");
					this.refreshConfigure();
					/*
					 * AutumnRestarter restater = AutumnRestarter.getInstance(); if (restater !=
					 * null) { restater.restart(); }
					 */
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 
	* 
	*
	 */
	private void refreshConfigure() {
		logger.info("正在刷新配置...");
		String[] keys = refreshEndpoint.refresh();
		if (keys != null) {
			for (String key : keys) {
				logger.info(key);
			}
		}
		logger.info("刷新配置完成...");
	}

}
